home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / SourceCode / AdobeExamples / NX_Patterns / DrawingView.m < prev    next >
Text File  |  1992-12-19  |  9KB  |  394 lines

  1.  
  2. /*
  3.  * (a)  (C) 1990 by Adobe Systems Incorporated. All rights reserved.
  4.  *
  5.  * (b)  If this Sample Code is distributed as part of the Display PostScript
  6.  *    System Software Development Kit from Adobe Systems Incorporated,
  7.  *    then this copy is designated as Development Software and its use is
  8.  *    subject to the terms of the License Agreement attached to such Kit.
  9.  *
  10.  * (c)  If this Sample Code is distributed independently, then the following
  11.  *    terms apply:
  12.  *
  13.  * (d)  This file may be freely copied and redistributed as long as:
  14.  *    1) Parts (a), (d), (e) and (f) continue to be included in the file,
  15.  *    2) If the file has been modified in any way, a notice of such
  16.  *      modification is conspicuously indicated.
  17.  *
  18.  * (e)  PostScript, Display PostScript, and Adobe are registered trademarks of
  19.  *    Adobe Systems Incorporated.
  20.  * 
  21.  * (f) THE INFORMATION BELOW IS FURNISHED AS IS, IS SUBJECT TO
  22.  *    CHANGE WITHOUT NOTICE, AND SHOULD NOT BE CONSTRUED
  23.  *    AS A COMMITMENT BY ADOBE SYSTEMS INCORPORATED.
  24.  *    ADOBE SYSTEMS INCORPORATED ASSUMES NO RESPONSIBILITY
  25.  *    OR LIABILITY FOR ANY ERRORS OR INACCURACIES, MAKES NO
  26.  *    WARRANTY OF ANY KIND (EXPRESS, IMPLIED OR STATUTORY)
  27.  *    WITH RESPECT TO THIS INFORMATION, AND EXPRESSLY
  28.  *    DISCLAIMS ANY AND ALL WARRANTIES OF MERCHANTABILITY, 
  29.  *    FITNESS FOR PARTICULAR PURPOSES AND NONINFRINGEMENT
  30.  *    OF THIRD PARTY RIGHTS.
  31.  */
  32.  
  33. /*
  34.  *    DrawingView.m
  35.  *
  36.  *    This view represents the page that the image is drawn onto. It is
  37.  *    a subview of the DocView. The DocView is the document view of
  38.  *    the ClipView. This view's real size grows with the scale but the
  39.  *    bounds always stays the same.
  40.  *
  41.  *    Version:    2.0
  42.  *    Author:    Ken Fromm
  43.  *    History:
  44.  *            03-07-91        Added this comment.
  45.  */
  46.  
  47. #import "DrawingView.h"
  48. #import "DocView.h"
  49. #import "PatternApp.h"
  50. #import "Pattern.h"
  51. #import "PSWDrawingWraps.h"
  52. #import "PSWpatterns.h"
  53.  
  54. #import <appkit/Button.h>
  55. #import <appkit/Cell.h>
  56. #import <appkit/Font.h>
  57. #import <appkit/Matrix.h>
  58. #import <appkit/Panel.h>
  59. #import <appkit/nextstd.h>
  60.  
  61. #import <objc/List.h>
  62. #import <dpsclient/dpsclient.h>
  63. #import <dpsclient/wraps.h>
  64.  
  65. static NXRect            fillRect = {120, 250, 300, 260};
  66.  
  67. @implementation DrawingView
  68.  
  69. - initPatterns
  70. {
  71.     float        matrix[6];
  72.  
  73.     patterns = [[List  alloc]  initCount:16];
  74.  
  75.     [self  lockFocus];
  76.     [patterns  addObject:[[Pattern  newPattern:"CircleStar"  size:0.72]
  77.                 definePatternRows:1 andCols:1]];
  78.     [patterns  addObject:[[Pattern  newPattern:"CircleStar"  size:0.72]
  79.                 definePatternRows:2 andCols:2]];
  80.     [patterns  addObject:[[Pattern  newPattern:"CircleStar"  size:0.72]
  81.                 definePatternRows:3 andCols:3]];
  82.     [patterns  addObject:[[Pattern  newPattern:"CircleStar"  size:0.72]
  83.                 definePatternRows:4 andCols:4]];
  84.  
  85.     /*    The color is set here because the image is made at this time
  86.     *    and we want it to be the same color as when used.
  87.     */
  88.     PSsetgray(NX_LTGRAY);
  89.     [patterns  addObject:[[Pattern  newPattern:"Octagon"  size:6]
  90.                 definePatternRows:1 andCols:1]];
  91.     [patterns  addObject:[[Pattern  newPattern:"Octagon"  size:6]
  92.                 definePatternRows:2 andCols:2]];
  93.     [patterns  addObject:[[Pattern  newPattern:"Octagon"  size:6]
  94.                 definePatternRows:3 andCols:3]];
  95.     [patterns  addObject:[[Pattern  newPattern:"Octagon"  size:6] 
  96.                 definePatternRows:4 andCols:4]];
  97.  
  98.     [patterns  addObject:[[Pattern  newPattern:"Brick"  size:24]
  99.                 definePatternRows:1 andCols:1]];
  100.     [patterns  addObject:[[Pattern  newPattern:"Brick"  size:24]
  101.                 definePatternRows:2 andCols:2]];
  102.     [patterns  addObject:[[Pattern  newPattern:"Brick"  size:24]
  103.                 definePatternRows:3 andCols:3]];
  104.     [patterns  addObject:[[Pattern  newPattern:"Brick"  size:24]
  105.                 definePatternRows:4 andCols:4]];
  106.  
  107.     matrix[0] = 2.59808; matrix[1] = 1.5; matrix[3] = -3.0; matrix[2] = matrix[4] = matrix[5] = 0.0;
  108.     [patterns  addObject:[[Pattern  newPattern:"Weave"  size:3  matrix:matrix]
  109.                 definePatternRows:1 andCols:1]];
  110.     [patterns  addObject:[[Pattern  newPattern:"Weave"  size:3  matrix:matrix]
  111.                 definePatternRows:2 andCols:2]];
  112.     [patterns  addObject:[[Pattern  newPattern:"Weave"  size:3  matrix:matrix]
  113.                 definePatternRows:3 andCols:3]];
  114.     [patterns  addObject:[[Pattern  newPattern:"Weave"  size:3  matrix:matrix]
  115.                 definePatternRows:4 andCols:4]];
  116.     [self  unlockFocus];
  117.  
  118.     font = [Font  newFont:"Times-Roman"  size:FONTSIZE  matrix:NX_IDENTITYMATRIX];
  119.     pattern = [patterns  objectAt:1];
  120.     [self  showPattern:self];
  121.  
  122.     return self;
  123. }
  124.  
  125. /*
  126. *    This method changes the title of the menu cell according to the
  127. *    value of the trace variable.
  128. */
  129. - trace:sender
  130. {
  131.     if (trace == NO)
  132.         [[sender selectedCell] setTitle:"Trace On"];
  133.     else
  134.         [[sender selectedCell] setTitle:"Trace Off"];
  135.  
  136.     trace = !trace;
  137.     
  138.     return self;
  139. }
  140.  
  141. - images:sender
  142. {
  143.     id            image;
  144.  
  145.     int            i, count;
  146.  
  147.     NXCoord        maxheight;
  148.  
  149.     NXPoint        position;
  150.  
  151.     NXRect        rect;
  152.  
  153.     SEL            aSelector;
  154.  
  155.     if (images == NO)
  156.     {
  157.         [[sender selectedCell] setTitle:"Hide Images"];
  158.         aSelector = @selector(orderFront:);
  159.     }
  160.     else
  161.     {
  162.         [[sender selectedCell] setTitle:"Show Images"];
  163.         aSelector = @selector(orderOut:);
  164.     }
  165.     images = !images;
  166.  
  167.     count = [patterns  count];
  168.     if (!imagesseen)
  169.     {
  170.         maxheight = 0.0;
  171.         position.x = position.y = 0.0;
  172.         for (i = 0; i < count; i++)
  173.         {
  174.             if (i % 8 == 0)
  175.             {
  176.                 position.x = 2.0;
  177.                 position.y += maxheight + 5;
  178.                 maxheight = 0;
  179.             }
  180.             image = [[patterns  objectAt:i]  image];
  181.             [image  moveTo:position.x  :position.y];
  182.             [image  setFloatingPanel:YES];
  183.  
  184.             [image  getFrame:&rect];
  185.             position.x += rect.size.width + 5;
  186.             maxheight = MAX(maxheight, rect.size.height);
  187.         }
  188.     
  189.         imagesseen = YES;
  190.     }
  191.     
  192.     for (i = 0; i < count; i++)
  193.         [[[patterns  objectAt:i]  image]  perform:aSelector  with:self];
  194.  
  195.     return self;
  196. }
  197.  
  198. - pattern:sender
  199. {
  200.     int            tag, num;
  201.  
  202.     tag = [[NXApp  patternMatrix]  selectedTag];
  203.     num = [[NXApp  countMatrix]  selectedTag];
  204.     pattern = [patterns  objectAt:((tag * NUM_PATTERNS) + num)];
  205.     [self  showPattern:self];
  206.  
  207.     [self  draw:self];
  208.  
  209.     return self;
  210. }
  211.  
  212. - draw:sender
  213. {
  214.     NXRect    visRect;
  215.  
  216.     eraseFields = NO;
  217.  
  218.     [self  resetFields:self];
  219.     [self  getVisibleRect:&visRect];
  220.     [self  display:&visRect :1];
  221.  
  222.     [self  displayFields:self];
  223.  
  224.     return self;
  225. }
  226.  
  227. - showPattern:sender
  228. {
  229.     id            box;
  230.  
  231.     NXPoint        pt;
  232.  
  233.     NXRect        boxRect;
  234.  
  235.     /*
  236.     *    Composite the pattern into the options panel.
  237.     */
  238.     box = [NXApp  patternBox];
  239.     [box  getBounds:&boxRect];
  240.     pt.x = (boxRect.origin.x + boxRect.size.width/2.0);
  241.     pt.y = (boxRect.origin.y + boxRect.size.height/2.0);
  242.  
  243.     [box  lockFocus]; 
  244.         [box  drawSelf:&boxRect  :1];
  245.         [pattern  compositeTo:pt.x  :pt.y];
  246.     [box  unlockFocus];
  247.     [[box  window]  flushWindow];
  248.  
  249.     return self;
  250. }
  251.  
  252. - eraseFields:sender
  253. {
  254.     id        matrixId;
  255.  
  256.     int        i;
  257.  
  258.     matrixId = [NXApp  timingMatrix];
  259.     for (i= 0; i < [matrixId cellCount]; i++)
  260.         [[matrixId cellAt:i :0] setStringValue:""];
  261.  
  262.     eraseFields = NO;
  263.  
  264.     return self;
  265. }
  266.  
  267. - resetFields:sender
  268. {
  269.     /* Prepare for next time around. */
  270.     timing.draws = timing.time = 0;
  271.  
  272.     return self;
  273. }
  274.  
  275. - displayFields:sender
  276. {
  277.      if (timing.draws)
  278.         [[[NXApp timingMatrix] cellAt:0 :0]  setIntValue:rint(timing.time/timing.draws)];
  279.  
  280.     [self  resetFields:self];
  281.     eraseFields = YES;
  282.  
  283.     return self;
  284. }
  285.  
  286. /*
  287. *    If the docview is zooming, then scale the drawing view.
  288. */
  289. - mouseDown:(NXEvent *)event
  290. {
  291.     NXPoint        p;
  292.     
  293.      p = event->location; 
  294.     if ([superview  isZooming])
  295.         return [nextResponder  scaleDrawViewToPoint:&p];
  296.  
  297.     return self;
  298. }
  299.  
  300. - showGraphics:(NXRect *)r
  301. {
  302.     NXRect        strokeRect, testRect;
  303.  
  304.     /*
  305.     *    For uncolored the pattern color should be set before setting it.
  306.     */
  307.     PSsetgray(NX_LTGRAY);
  308.  
  309.     /*
  310.     *    Sets the pattern. (Places the pattern dictionary on
  311.     *    the dictionary stack.)
  312.     */
  313.     [pattern set];
  314.  
  315.     if ([[[NXApp graphicsMatrix]  findCellWithTag:STROKE]  state])
  316.     {
  317.         strokeRect = fillRect;
  318.         NXInsetRect(&strokeRect, -30, -30);
  319.         testRect = strokeRect;
  320.         NXInsetRect(&testRect, -STROKEWIDTH/2.0, -STROKEWIDTH/2.0);
  321.         if (NXIntersectsRect(r, &testRect))
  322.         {    
  323.             PSsetlinewidth(STROKEWIDTH);
  324.             PSrectstroke(NX_X(&strokeRect), NX_Y(&strokeRect),
  325.                     NX_WIDTH(&strokeRect), NX_HEIGHT(&strokeRect));
  326.         }
  327.     }
  328.  
  329.     if ([[[NXApp graphicsMatrix]  findCellWithTag:FILL]  state])
  330.     {
  331.         if (NXIntersectsRect(r, &fillRect))
  332.         {
  333.             PSrectfill(NX_X(&fillRect), NX_Y(&fillRect),
  334.                     NX_WIDTH(&fillRect), NX_HEIGHT(&fillRect));
  335.         }
  336.     }
  337.  
  338.     if ([[[NXApp graphicsMatrix]  findCellWithTag:TEXT]  state])
  339.     {
  340.         [font  set];
  341.         NXSetRect(&testRect, 0, 0, 600, 250);
  342.         if (NXIntersectsRect(r, &testRect))
  343.         {
  344.             PSmoveto(3, 50);
  345.             PSshow("Adobe");
  346.         }
  347.     }
  348.     
  349.     /*
  350.     *    Important - This method is important with this implementation
  351.     *    of pattern emulation.
  352.     */
  353.     [pattern unset];
  354.  
  355.     return self;
  356. }
  357.  
  358. /*
  359. *    Compare the bounds of the object with the rectangle to draw in order to
  360. *    eliminate unnecessary drawing.
  361. */
  362. - drawSelf:(NXRect *)r :(int) count
  363. {
  364.     int            ElapsedTime;
  365.  
  366.     if ([[NXApp  lockMatrix]  selectedTag] != LOCK_WINDOW)
  367.         PSWSetPatternView();
  368.  
  369.     if (eraseFields)
  370.         [self  eraseFields:self];
  371.  
  372.     PSsetgray(NX_WHITE);
  373.     NXRectFill(r);
  374.     NXRectClip(r);
  375.  
  376.     PSsetwindowtype(NX_RETAINED, [window  windowNum]);
  377.     if (trace)
  378.         DPSTraceContext(DPSGetCurrentContext(), YES);
  379.  
  380.     PSWMarkTime();  NXPing();
  381.         [self  showGraphics:r];
  382.     PSWReturnTime(&ElapsedTime);
  383.     timing.time += ElapsedTime;
  384.     timing.draws++;
  385.  
  386.     if (trace)
  387.         DPSTraceContext(DPSGetCurrentContext(), NO);
  388.     PSsetwindowtype(NX_BUFFERED, [window  windowNum]);
  389.  
  390.     return self;
  391. }
  392.  
  393. @end
  394.